package com.heo.protocol.packet;

import org.apache.log4j.Logger;

import com.heo.util.ByteHelper;

public class UploadLogReq extends BasePacket
{
  private int BODY_LENGTH = -1;// 包体长未知
  private static final String PACKET_NAME = "[上传日志请求包]";
  private String logContent = null;// 日志内容

  private static Logger log = Logger.getLogger(UploadLogReq.class);

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public UploadLogReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  public UploadLogReq(String logContent) throws Exception
  {
    super();
    super.commandId = DeviceCommand.UPLOAD_LOG_REQ;
    super.sequenceId = 0x01;
    this.logContent = logContent;
    super.totalLength = HEAD_LENGTH + logContent.length();
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + logContent.length();
  }

  @Override
  public byte[] getPacketBody()
  {
    return logContent.getBytes();
  }

  @Override
  public void makeBody() throws Exception
  {
//    if (packetBody.length != BODY_LENGTH)
//    {
//      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
//      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
//    }
    logContent = ByteHelper.byteToString(packetBody, 0, packetBody.length, (byte) 0);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public static void main(String[] args)
  {

  }

  public String getLogContent()
  {
    return logContent;
  }

  public void setLogContent(String logContent)
  {
    this.logContent = logContent;
  }

}
